package com.csdj.web;

import com.csdj.entity.Employee;
import com.csdj.entity.PageInfo;
import com.csdj.service.EmployeeService;
import com.csdj.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * packageName com.csdj.web
 *
 * @author winver
 * @version 1.0
 * @className QueryEmpServlet
 * @date 2024/11/27
 * @description TODO
 */
@WebServlet("/queryEmp")
public class QueryEmpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码设置
        resp.setContentType("text/html;charset=utf-8");
        //获取参数
        String name = req.getParameter("name");
        if(name == null){
            name = "";
        }
        String depteidStr = req.getParameter("depteid");
        int deptId = -1;
        if(depteidStr != null && !"".equals(depteidStr)){
            deptId = Integer.parseInt(depteidStr);
        }
        //分页参数
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        int pageNum = 1;
        int pageSize = 5;
        if(pageNoStr != null && !"".equals(pageNoStr)){
            pageNum = Integer.parseInt(pageNoStr);
        }
        if(pageSizeStr != null && !"".equals(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        System.out.println("name111111111"+name);
        System.out.println(deptId);


        //调用业务层
        EmployeeService employeeService = new EmployeeServiceImpl();
        PageInfo<Employee> pageInfo = employeeService.getPageEmp(pageNum, pageSize, name, deptId);

        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/index.jsp").forward(req,resp);

    }
}
